package com.hsxc.cdz.biz.reserve;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.hsxc.cdz.base.bean.Reserv;

/**
 * @author 作者 : 陈钊
 * @version 创建时间：2016年9月10日 下午5:34:13 类说明：
 */
public class CacheReservManager {
    // ------------------活跃的预约记录操作----------------------
    // 一个用户只能有一个活跃的预约记录
    public static int SUCCESS = 0;
    public static int FAIL = 1;
    private Map<String, Reserv> reservs = new ConcurrentHashMap<>();

    /**
     * @param reserv
     * @return 成功0失败1
     */
    public int addReserv(Reserv reserv) {
        reservs.put(reserv.getResNo(), reserv);
        return SUCCESS;
    }

    /**
     * 删除预约
     *
     * @param resNo
     * @return 成功0 失败1
     */
    public int removeReserv(String resNo) {
        reservs.remove(resNo);
            return SUCCESS;
    }

    /**
     * 查找活跃的预约记录
     *
     * @param userNo 20位数字
     *               用户编号
     * @return 查到则返回该记录对象，否则null
     */
    public Reserv queryReserv(String cardNo, String pileNo) {
        Iterator<Entry<String, Reserv>> entries = reservs.entrySet().iterator();
        while (entries.hasNext()) {
            Entry<String, Reserv> entry = entries.next();
            Reserv reserv = entry.getValue();
            //if (reserv.getUserNo().equals(cardNo)&&reserv.getPileNo().equals(pileNo))
            if (reserv.getUserNo().equals(cardNo))
                return reserv;
        }
        return null;
    }

    public Reserv queryReservByPileNo(String pileNo) {
        Iterator<Entry<String, Reserv>> entries = reservs.entrySet().iterator();
        while (entries.hasNext()) {
            Entry<String, Reserv> entry = entries.next();
            Reserv reserv = entry.getValue();
            if (reserv.getPileNo().equals(pileNo))
                return reserv;
        }
        return null;
    }
}
